package cn.bj.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

/**
 * 服务端推送: 每5s钟向浏览器推送随机消息
 * SSE: Server Send Event : 服务端发送事件
 */
@Controller
public class SseController {

    @RequestMapping(value = "/push", produces = "text/event-stream;charset=UTF-8")
    @ResponseBody
    public String push() {
        Random random = new Random();
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "data:Testing 1,2,3 " + random.nextInt() + "\n";

    }

}
